package com.ruoyi.common.core.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangpeng
 */
public class BeanCopyUtils {

    /**
     * 集合拷贝 浅复制
     *
     * @param source 数据源
     * @param clazz  复制class对象
     * @return 复制对象集合
     */
    public static <E, V> List<V> copyList(final List<E> source, final Class<V> clazz) {
        return source.stream().map(pojo -> BeanUtil.copyProperties(pojo, clazz)).collect(Collectors.toList());
    }

    /**
     * 分页对象拷贝 浅复制
     *
     * @param page  分页数据源
     * @param clazz 复制class对象
     * @return 复制分页对象
     */
    public static <E, V> Page<V> copyPage(final Page<E> page, final Class<V> clazz) {
        Page<V> vPage = new Page<>();
        BeanUtil.copyProperties(page, vPage, "records");
        vPage.setRecords(page.getRecords().stream().map(pojo -> BeanUtil.copyProperties(pojo, clazz)).collect(Collectors.toList()));
        return vPage;
    }

    /**
     * 拷贝实体类
     *
     * @param source           数据源
     * @param clazz            复制class对象
     * @param ignoreProperties 不拷贝的的属性列表
     * @return 复制对象集合
     */
    public static <E, V> V copyBean(final E source, final Class<V> clazz, String... ignoreProperties) {
        return BeanUtil.copyProperties(source, clazz, ignoreProperties);
    }

    /**
     * 拷贝实体类
     *
     * @param source           数据源
     * @param target           复制目标对象
     * @param ignoreProperties 不拷贝的的属性列表
     */
    public static void copyBean(final Object source, final Object target, String... ignoreProperties) {
        BeanUtil.copyProperties(source, target, ignoreProperties);
    }

    /**
     * 拷贝实体类：不拷贝："id","createBy","createTime","updateBy","updateTime"
     *
     * @param source           数据源
     * @param clazz            复制class对象
     * @param ignoreProperties 不拷贝的的属性列表
     * @return 复制对象集合
     */
    public static <E, V> V copyNewBean(final E source, final Class<V> clazz, String... ignoreProperties) {
        String[] arr = {"id", "createBy", "createTime", "updateBy", "updateTime"};
        arr = ArrayUtil.addAll(arr, ignoreProperties);
        return BeanUtil.copyProperties(source, clazz, arr);
    }

    /**
     * 拷贝实体类：不拷贝："id","createBy","createTime","updateBy","updateTime"
     *
     * @param source           数据源
     * @param target           复制目标对象
     * @param ignoreProperties 不拷贝的的属性列表
     */
    public static void copyNewBean(final Object source, final Object target, String... ignoreProperties) {
        String[] arr = {"id", "createBy", "createTime", "updateBy", "updateTime"};
        arr = ArrayUtil.addAll(arr, ignoreProperties);
        BeanUtil.copyProperties(source, target, arr);
    }
}
